package org.mogu.control;

import java.io.IOException;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.mogu.R;
import org.mogu.Welcome;
import org.mogu.staticvalue.Convert;
import org.mogu.tools.Imgtools;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Weibo11 extends RelativeLayout {
	TextView weibocontent;
	EditText edittext;
	Button cancelbtn;
	Button replaybtn;
	Button sendbtn;
	Button guanbtn;
	Weibo11 weibocontent10;
	String code;
	String replayType;
	String maincode;
	Welcome welcome;

	DefaultHttpClient client;
	HttpResponse httpResponse;

	public Weibo11(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public Weibo11(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater.from(context).inflate(R.layout.weibo11, this, true);
		weibocontent = (TextView) findViewById(R.id.weibo10content);
		edittext = (EditText) findViewById(R.id.weibo10text);
		cancelbtn = (Button) findViewById(R.id.weibo_cancel);
		cancelbtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				welcome.goBack();
			}
		});
		guanbtn = (Button) findViewById(R.id.weibo_guanzhu);
		guanbtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				try {
					client = new DefaultHttpClient();
					String url;
					url = Convert.hosturl + "/AddReply?UserName="
							+ Convert.uname + "&UserPwd=" + Convert.upwd+"&Code="+code+"&MainCode="+maincode+"&Type="+replayType+"&Content=**1****";
					HttpGet getMethod = new HttpGet(url+ Convert.testNum);

					httpResponse = client.execute(getMethod);

					if (httpResponse.getStatusLine().getStatusCode() == 200) {
						// 取出回应字串
						String strResult;
						strResult = EntityUtils.toString(httpResponse
								.getEntity());
						if ("1".equals(strResult)) {
							Toast.makeText(welcome, "添加关注失败，稍后再试。", 8000).show();
						}
						if ("0".equals(strResult)) {
							Toast.makeText(welcome, "添加关注成功。", 8000).show();
						}
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}finally{
					welcome.goBack();
				}
				
			}
		});
		sendbtn = (Button) findViewById(R.id.weibo_resend);
		sendbtn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				try {
					client = new DefaultHttpClient();
					String url;
					url = Convert.hosturl + "/AddReply?UserName="
							+ Convert.uname + "&UserPwd=" + Convert.upwd+"&Code="+code+"&MainCode="+maincode+"&Type="+replayType+"&Content=**2**"+URLEncoder.encode(getEditText(),"UTF-8")+"**";
					HttpGet getMethod = new HttpGet(url+ Convert.testNum);
					
					httpResponse = client.execute(getMethod);
					
					if (httpResponse.getStatusLine().getStatusCode() == 200) {
						// 取出回应字串
						String strResult;
						strResult = EntityUtils.toString(httpResponse
								.getEntity());
						if ("1".equals(strResult)) {
							Toast.makeText(welcome, "转发失败，稍后再试。", 8000).show();
						}
						if ("0".equals(strResult)) {
							Toast.makeText(welcome, "转发成功。", 8000).show();
						}
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}finally{
					welcome.goBack();
				}
				
			}
		});
		replaybtn = (Button) findViewById(R.id.weibo_replay);
		replaybtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				try {
					client = new DefaultHttpClient();
					String url;
					url = Convert.hosturl + "/AddReply?UserName="
							+ Convert.uname + "&UserPwd=" + Convert.upwd+"&Code="+code+"&MainCode="+maincode+"&Type="+replayType+"&Content=**3**"+URLEncoder.encode(getEditText(),"UTF-8")+"**";
					HttpGet getMethod = new HttpGet(url+ Convert.testNum);

					httpResponse = client.execute(getMethod);

					if (httpResponse.getStatusLine().getStatusCode() == 200) {
						// 取出回应字串
						String strResult;
						strResult = EntityUtils.toString(httpResponse
								.getEntity());
						if ("1".equals(strResult)) {
							Toast.makeText(welcome, "评论失败，稍后再试。", 8000).show();
						}
						if ("0".equals(strResult)) {
							Toast.makeText(welcome, "评论成功。", 8000).show();
						}
					}
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}finally{
					welcome.goBack();
				}
			}
		});
	}

	public void setContent(String code, String replayType, String maincode,
			 String content) {
		weibocontent.setText(Imgtools.getContent(content,weibocontent));
		this.code = code;
		this.replayType = replayType;
		this.maincode = maincode;
		edittext.setText("");
	}

	public String getEditText() {
		return edittext.getText().toString();
	}

	public void setWeibo11(Weibo11 content, Welcome welcome) {
		weibocontent10 = content;
		this.welcome = welcome;
	}

}
